package com.iseastar.guojiang.register;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.RegisterCourierMaterialBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import java.util.ArrayList;

/**
 * 注册火箭侠物料展示
 * Created by lidandan on 2018/4/28.
 */
public class RegisterCourierMaterialActivity extends BaseActivity2 {
    private UserBean item;
    private ListView mListview;
    private RegisterCourierMaterialBean items;

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        item = (UserBean) getIntent().getSerializableExtra("item");
        if (item == null) {
            item = AppCache.getUser();
        }
        super.onCreate(savedInstanceState);
        AppHttp.getInstance().getCourierMaterial();
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.register_courier_material_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("物料");
        mListview = (ListView) findViewById(R.id.listview);
        findViewById(R.id.pay_a_deposit_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (items == null) {
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getContext(), RegisterMaterialPayActivity.class);
                intent.putExtra("item", item);
                intent.putExtra("deposit", items.getDeposit());
                startActivityForResult(intent, 1503);
            }
        });
    }


    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //物料请求返回
            case MsgID.get_courier_material:
                final ReqResult<RegisterCourierMaterialBean> result = JSON.parser(msg.obj, RegisterCourierMaterialBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            items = result.getResult();
                            ArrayList<RegisterCourierMaterialBean.MaterialBean> list = new ArrayList<>();

                            if (items != null) {
                                if (items.getBox() != null) {
                                    list.add(items.getBox());
                                }
                                if (items.getHelmet() != null) {
                                    list.add(items.getHelmet());
                                }
                                if (items.getVest() != null) {
                                    list.add(items.getVest());
                                }
                                if (items.getWorkCard() != null) {
                                    list.add(items.getWorkCard());
                                }

                            }
                            RegisterCourierMaterialAdapter adapter = new RegisterCourierMaterialAdapter(list, getContext());
                            mListview.setAdapter(adapter);
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && 1503 == requestCode) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }
}
